#!/bin/sh
set -m

CURRENT_PATH=`dirname $(readlink -f $0)`

CONF_PATH=$CURRENT_PATH/conf/jlogstash.yml

IMPUT_CODE=$1

HO_HEAP_SIZE="${HO_HEAP_SIZE:=300m}"

JAVA_OPTS="$JAVA_OPTS -Xmx${HO_HEAP_SIZE}"

JAVA_OPTS="$JAVA_OPTS -Xms${HO_HEAP_SIZE}"

JAVA_OPTS="$JAVA_OPTS -server"

JAVA_OPTS="$JAVA_OPTS -Xloggc:$CURRENT_PATH/logs/gc.log"

JAVA_OPTS="$JAVA_OPTS -XX:HeapDumpPath=$CURRENT_PATH/logs/heapdump.hprof"

JAVA_OPTS="$JAVA_OPTS -XX:+UseG1GC -XX:MaxGCPauseMillis=20 -XX:InitiatingHeapOccupancyPercent=35 -XX:+HeapDumpOnOutOfMemoryError -XX:+DisableExplicitGC -Dfile.encoding=UTF-8 -Djna.nosys=true -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps"

start ()
{
   echo "$CONF_PATH starting ............"
   exec java $JAVA_OPTS -cp $CURRENT_PATH/lib/* com.dtstack.jlogstash.JlogstashMain -f $CONF_PATH > $CURRENT_PATH/logs/out.log 2>&1&
}
stop ()
{
    PID=$(ps -ef|grep java |grep "$CONF_PATH" |grep -v grep  |awk '{print $2}')
    if [ -n "$PID" ];then
	#关闭脚本
        echo "$CONF_PATH kill pid=$PID"
        kill -9 $PID
    fi
}
status()
{
	#查看是否运行中
    PID=$(ps -ef|grep java |grep "$CONF_PATH" |grep -v grep  |awk '{print $2}')
    if [ -n "$PID" ];then
	echo "$CONF_PATH is running pid=$PID"
    else
	echo "$CONF_PATH is stop...."
    fi

}

#默认重启服务
if [[ -z "$1" ]];then
   IMPUT_CODE=restart
fi


case "$IMPUT_CODE" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        start
        ;;
  status)
	status
	;;
  *)
echo "Usage: $SCRIPTNAME {start|stop|restart|status}" >&2
exit 1
;;
esac
exit 0
